package com.xyht.medicine_management_system.modules.coupon.controller;


import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.modules.coupon.entity.req.MmsCouponReq;
import com.xyht.medicine_management_system.modules.coupon.service.MmsCouponService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 优惠劵表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-11-29
 */
@RestController
@SecretBody
@RequestMapping("/coupon")
public class MmsCouponController {
    @Resource
    private MmsCouponService mmsCouponService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("添加优惠券")
    @PostMapping("/add")
    public ResponseResult addCoupon(@RequestBody MmsCouponReq couponReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsCouponService.addCoupon(couponReq, userId);
    }

    @ApiModelProperty("删除优惠券")
    @PostMapping("/del")
    public ResponseResult delCoupon(@RequestBody MmsCouponReq couponReq) {
        return mmsCouponService.delCoupon(couponReq);
    }

    @ApiModelProperty("修改优惠券")
    @PostMapping("/update")
    public ResponseResult updateCoupon(@RequestBody MmsCouponReq couponReq) {
        return mmsCouponService.updateCoupon(couponReq);
    }

    @ApiModelProperty("优惠券列表")
    @GetMapping("/list")
    public ResponseResult listCoupon(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "searchStr", required = false) String searchStr) {
        return mmsCouponService.listCoupon(pageNum, pageSize, searchStr);
    }

    @ApiModelProperty("优惠券详情")
    @GetMapping("/detail")
    public ResponseResult couponDetail(@RequestParam("couponId") String couponId) {
        return mmsCouponService.couponDetail(couponId);
    }

    @ApiOperation("用户可领取优惠券列表")
    @GetMapping("/userCanGetList")
    public ResponseResult userCanGetList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "searchStr", required = false) String searchStr,
                                         HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsCouponService.userCanGetList(pageNum, pageSize, searchStr, userId);
    }
}

